package com.demo.exception;

import com.demo.tool.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//日志
@Slf4j
//用来处理全局异常
@RestControllerAdvice
public class GlobalExceptionHandler {

    //统一处理方法抛出的异常
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public ReturnResult handlerException(Exception e) {
        log.error(e.getMessage());
        return ReturnResult.error(e.getMessage()+"服务器出错");
    }


    /**
     * 自定义异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ServiceException.class)
    public ReturnResult handleException(ServiceException e) {
        log.error(e.getMessage());
        return ReturnResult.error(e.getMessage(), e.getCode());
    }

}
